package com.ticket.sass.admin.dto.output.commodity;


import com.ticket.sass.admin.model.KeyInfoModel;
import com.ticket.sass.common.entity.tenant.CommodityEntity;
import com.ticket.sass.common.entity.tenant.CommoditySkuEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ywb
 * @date 2023-04-06 15:54
 */
@Data
@ApiModel("卖品")
public class CommodityInfoOutput {
    @ApiModelProperty(value = "卖品id", required = true)
    private Long id;
    @ApiModelProperty(value = "封面图")
    private List<KeyInfoModel> cover;
    @ApiModelProperty(value = "卖品标题", required = true)
    private String name;
    @ApiModelProperty(value = "卖品描述", required = true)
    private String desc;
    
    @ApiModelProperty(value = "发布状态：0 未发布，1已发布")
    private Short publishStatus;
    
    @ApiModelProperty(value = "分类ID")
    private Long categoryId;
    
    @ApiModelProperty(value = "分类名")
    private String categoryName;
    
    @ApiModelProperty(value = "最小售价 sku 中售价最小的", required = true)
    private BigDecimal minSalePrice;
    @ApiModelProperty(value = "最大售价 sku 中售价最大的", required = true)
    private BigDecimal maxSalePrice;
    
    @ApiModelProperty(value = "最小原价 sku 中原价最小的", required = true)
    private BigDecimal minMarketPrice;
    @ApiModelProperty(value = "最大原价 sku 中原价最大的", required = true)
    private BigDecimal maxMarketPrice;
    
    @ApiModelProperty(value = "库存 所有sku 库存总和", required = true)
    private Integer stockNum;
    
    @ApiModelProperty(value = "已售 所有sku 已售总和", required = true)
    private Integer soldNum;
    
    @ApiModelProperty(value = "核销数 已售总和", required = true)
    private Integer useNum = 0;
    
    @ApiModelProperty(value = "创建时间", required = true)
    public LocalDateTime createdAt;
    
    @ApiModelProperty(value = "聚合属性Map", required = true)
    private Map<String, Set<String>> aggregatedAttrMap;
    
    @ApiModelProperty(value = "卖品规格", required = true)
    private List<CommoditySkuOutput> commoditySkus;
    
    @ApiModelProperty(value = "核销方式 1 平台核销")
    private Short useMethod;
    
    @ApiModelProperty(value = "售后 1 支持退款 2 不支持退款")
    private Short refund;
    
    public CommodityInfoOutput(Long id, List<KeyInfoModel> cover, String name, String desc, Short publishStatus, Long categoryId, String categoryName,
                               BigDecimal minSalePrice, BigDecimal maxSalePrice, BigDecimal minMarketPrice,
                               BigDecimal maxMarketPrice, Integer stockNum, Integer soldNum, LocalDateTime createdAt) {
        this.id = id;
        this.cover = cover;
        this.name = name;
        this.desc = desc;
        this.publishStatus = publishStatus;
        this.categoryId = categoryId;
        this.categoryName = categoryName;
        this.minSalePrice = minSalePrice;
        this.maxSalePrice = maxSalePrice;
        this.minMarketPrice = minMarketPrice;
        this.maxMarketPrice = maxMarketPrice;
        this.stockNum = stockNum;
        this.soldNum = soldNum;
        this.createdAt = createdAt;
        
        this.useMethod = 1;
        this.refund = 1;
    }
    
    public CommodityInfoOutput(Long id, List<KeyInfoModel> cover, String name, String desc, Long categoryId, String categoryName) {
        this.id = id;
        this.cover = cover;
        this.name = name;
        this.desc = desc;
        this.categoryId = categoryId;
        this.categoryName = categoryName;
    }
    
    public static CommodityInfoOutput buildFromEntities(CommodityEntity commodity, List<CommoditySkuEntity> skus) {
        Long id = commodity.getId();
        List<KeyInfoModel> cover = KeyInfoModel.generationList(commodity.getCover());
        String name = commodity.getName();
        Short publishStatus = commodity.getPublishStatus() != null ? commodity.getPublishStatus().getValue() : 0;
        String desc = commodity.getDesc();
        Long categoryId = commodity.getCategoryId();
        String categoryName = commodity.getCategoryName();
        
        if (skus.size() == 0){
            return new CommodityInfoOutput(id, cover, name, desc, categoryId, categoryName);
        }
        
        BigDecimal minSalePrice = skus.get(0).getSalePrice();
        BigDecimal maxSalePrice = skus.get(0).getSalePrice();
        BigDecimal minMarketPrice = skus.get(0).getMarketPrice();
        BigDecimal maxMarketPrice = skus.get(0).getMarketPrice();
        Integer stockNum = 0;
        Integer soldNum = 0;
        
        for (CommoditySkuEntity sku : skus) {
            if (sku.getSalePrice().compareTo(minSalePrice) < 0) {
                minSalePrice = sku.getSalePrice();
            }
            if (sku.getSalePrice().compareTo(maxSalePrice) > 0) {
                maxSalePrice = sku.getSalePrice();
            }
            if (sku.getMarketPrice().compareTo(minMarketPrice) < 0) {
                minMarketPrice = sku.getMarketPrice();
            }
            if (sku.getMarketPrice().compareTo(maxMarketPrice) > 0) {
                maxMarketPrice = sku.getMarketPrice();
            }
            stockNum += sku.getStockNum();
            soldNum += sku.getSoldNum();
        }
        
        // 控制BigDecimal的值最多显示小数点后两位
        minSalePrice = minSalePrice.setScale(2, RoundingMode.HALF_UP);
        maxSalePrice = maxSalePrice.setScale(2, RoundingMode.HALF_UP);
        minMarketPrice = minMarketPrice.setScale(2, RoundingMode.HALF_UP);
        maxMarketPrice = maxMarketPrice.setScale(2, RoundingMode.HALF_UP);
        
        return new CommodityInfoOutput(id, cover, name, desc, publishStatus, categoryId, categoryName, minSalePrice, maxSalePrice,
            minMarketPrice, maxMarketPrice, stockNum, soldNum, commodity.getCreatedAt());
    }
}
